import service from './request0.js';

// 获取购物车商品项
export const fetchCartItems = async (userId) => {
    console.log('传递的 userId:', userId); // 打印 userId 的值
    try {
      const response = await service.get(`/cartItem/user/${userId}`);
      console.log('获取购物车成功22:', response.data);
      return response;
      
    } catch (error) {
      console.error('获取购物车商品项失败:', error);
      throw error;
    }
  };
  

// 添加商品到购物车
export const addCartItem = async (item) => {
  try {
    const response = await service.post(`/cartItem`, item);
    return response;
  } catch (error) {
    console.error('添加商品到购物车失败:', error);
    throw error;
  }
};
 
// 更新购物车商品项
export const updateCartItem = async (updateData) => {
  try {
    const response = await service.put(`/cartItem`, updateData);
    console.log('更新购物车商品项成功:', updateData);
    return response;
  } catch (error) {
    console.error('更新购物车商品项失败:', error);
    throw error;
  }
};

// 移除购物车商品项
export const removeCartItem = async (cartItemId) => {
  try {
    const response = await service.delete(`/cartItem/${cartItemId}`);
    return response;
  } catch (error) {
    console.error('移除购物车商品项失败:', error);
    throw error;
  }
};